/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hfy.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 *
 * @author JOHN
 */
public class JDBCUtil {
    private static ConfigBean cb = null;
    private static Connection conn = null;
    public static ConfigBean getConfigBean(){
        if(cb == null){
            cb = new ConfigBean();
        }
        return cb;
    }
    
    public static void setConfigBean(ConfigBean configBean){
        cb = configBean;
    }
    
    
        /**
	 * 取得数据库的连接
	 * @return 一个数据库的连接
	 */
        public static Connection getConnection(){
            if(conn!=null){
                return conn;
            }
             try {
                //初始化驱动类com.mysql.jdbc.Driver
                Class.forName(cb.driver);
                conn = DriverManager.getConnection(cb.url,cb.user, cb.password);
                //该类就在 mysql-connector-java-5.0.8-bin.jar中,如果忘记了第一个步骤的导包，就会抛出ClassNotFoundException
            } catch (ClassNotFoundException e) { 				
                e.printStackTrace();
            }catch (SQLException e) {							
                e.printStackTrace();
            }
            return conn;
        }
    
    
    public static class ConfigBean{
        
        String driver=null;
        String url = null;
        String user = null;
        String password = null;

        protected ConfigBean(){
            
        }

        
        public String getDriver() {
            return driver;
        }

        public void setDriver(String driver) {
            this.driver = driver;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getUser() {
            return user;
        }

        public void setUser(String user) {
            this.user = user;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }
        
    }
}
